package com.example.logining.service.impl;

import com.example.logining.domain.pojo.SysUser;
import com.example.logining.domain.vo.LoginUser;
import com.example.logining.mapper.UserMapper;
import com.example.logining.domain.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @title: UserDetailsServiceImpl
 * @author: wywang
 * @date: 2024/11/10
 * @description:
 **/
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userMapper.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }
        // 从数据库获取用户权限信息
        List<String> permissions = userMapper.selectUserPermissions(user.getId());

        // 返回一个 Spring Security 的 UserDetails 对象
        return new LoginUser(user, permissions);
    }
}
